package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
@Slf4j
public class UpDownListener {
    @Autowired
    private IApArticleService articleService;

    /**
     * 监听文章上下架
     * @param record
     */
    @KafkaListener(topics = "${topic.upDownTopic}")
    public void handlerMsg(ConsumerRecord<String, String> record) {
        String value = record.value();
        if (value != null) {
            log.info("监听到文章上下架的消息");
            HashMap hashMap = JSON.parseObject(value, HashMap.class);
            Long articleId = (Long) hashMap.get("articleId");
            boolean isDown = (boolean) hashMap.get("isDown");

            LambdaUpdateWrapper<ApArticle> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(ApArticle::getId,articleId);
            wrapper.set(ApArticle::getIsDown,isDown);
            articleService.update(wrapper);

        }
    }

}
